× Introduction Applying CSS CSS Syntax CSS Selectors Grouping Color Background CSS Fonts CSS Text CSS Links Gradients The Box Model Margin and Padding Border Outline Measurement Units Dimension CSS Quick Reference Examples Projects eBooks






CSS Quick Reference

CSS stands for Cascading Style Sheets, is the language we use to style a Web page.


Cascading Style Sheets

CSS, which stands for Cascading Style Sheets, is used for describing the presentation (i.e. the layout and formatting) of the web pages. CSS handles the look and feel part of a web page. Using CSS, you can control the colorof the text, the style of fonts, the spacing between paragraphs, how columns are sized and laid out, what background images or colors are used, as well as a variety of othereffects.

CSS was introduced in 1996 by the World Wide Web Consortium (W3C), which also maintains its standard. CSS was designed to enable the separation of presentation and content. Now web designers can move the formatting information of the web pages to a separate style sheet which results in considerably simpler HTML markup, and better maintainability. CSS3 is the latest version of the CSS specification. CSS3 adds several new styling features and improvements to enhance the web presentation capabilities.

As one of the core languages of the Web, CSS is used today by almost all web sites to enhance the web experience. It has been a revolution in the World Wide Web and is a must-learn language for anyone working with web design. Like HTML, the CSS language is easy to learn and use.

What You Can Do with CSS

There are lot more things you can do with CSS.

  • You can easily apply same style rules on multiple elements.
  • You can control the presentation of multiple pages of a website with a single style sheet.
  • You can present the same page differently on different devices.
  • You can style dynamic states of elements such as hover, focus, etc. that isn't possible otherwise.
  • You can change the position of an element on a web page without changing the markup.
  • You can alter the display of existing HTML elements.
  • You can transform elements like scale, rotate, skew, etc. in 2D or 3D space.
  • You can create animations and transitions effects without using any JavaScript.
  • You can create print friendly version of your web pages.

Advantages of CSS

  • CSS saves time - You can write CSS once and then reuse the same sheet in multiple HTML pages. You can define a style for each HTML element and apply it to as many web pages as you want.
  • Pages load faster - If you are using CSS, you do not need to write HTML tag attributes every time. Just write one CSS rule of a tag and apply it to all the occurrences of that tag. So, less code means faster download times.
  • Easy maintenance - To make a global change, simply change the style, and all the elements in all the web pages will be updated automatically.
  • Superior styles to HTML - CSS has a much wider array of attributes than HTML, so you can give a far better look to your HTML page in comparison to HTML attributes.
  • Multiple Device Compatibility - Style sheets allow content to be optimized for more than one type of device. By using the same HTML document, different versions of a website can be presented for handheld devices such as PDAs and cellphones or for printing.
  • Global web standards – Now HTML attributes are being deprecated and it is being recommended to use CSS. So it’s a good idea to start using CSS in all the HTML pages to make them compatible with future browsers.

Using HTML with CSS

In this tutorial you will learn how to apply style rules to HTML elements.

CSS provides styles to HTML elements on the page. Inline styling involves usage of the style attribute in tags, and is highly discouraged. Internal stylesheets use the <style>tag and are used to declare rules for directed portions ofthe page. External stylesheets may be used through a <link> tag which takes an external file of CSS and applies therules to the document.

  • Inline styles — Using the style attribute in the HTML start tag.
  • Internal styles — Using the <style> element in the head section of the document.
  • External style sheet — Using the <link> element, pointing to an external CSS files.

Note: The inline styles have the highest priority, and the external style sheets have the lowest. It means if you specify styles for your paragraphs in both embedded and external style sheets, the conflicting style rules in the embedded style sheet would override the external style sheet.

A Simple HTML Document withh CSS:   Try it  

<!DOCTYPE html>
<html>
<head>
<title>My First HTML Page</title>
</head>
<body>
<h1 style ="color:blue;">Welcome!</h1>
<p>Paragraph</p>
</body>
</html>
<p>

CSS styles inclusion in HTML

Sl.No. Function Code Reference
1

Inline CSS- The style Attribute

You can use style attribute of any HTML element to define style rules. These rules will be applied to that element only. The style attribute includes a series of CSS property and value pairs. Each property: value pair is separated by a semicolon (;)
<h1 style ="color: blue;">This is inline CSS</h2>
<h1 style ="color: blue;background-color:yellow; ">This is inline CSS</h1>
Try it  
2

Internal CSS - The <style> Element

You can put your CSS rules into an HTML document using the <style> element. This tag is placed inside the <head>...</head> tags. Rules defined using this syntax will be applied to all the elements available in the document.
<head>
<style>
h1{
background-color: blue;
color: yellow;
}
</style>
</head>
Try it  
3

External CSS

The <link> element can be used to include an external stylesheet file in your HTML document. An external style sheet is a separate text file with .css extension. You define all the Style rules within this text file and then you can include this file in any HTML document using <link> element.
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
Try it  


CSS Syntax

Sl.No. Function Code Reference
1

CSS style rule

A CSS stylesheet consists of a set of rules that are interpreted by the web browser and then applied to the corresponding elements such as paragraphs, headings, etc. in the document. A style rule is made of three parts: selector, property and value.

In the example above p is a selector, color is the CSS property, and the green is the values of the property. Each declaration consists of a property and a value separated by a colon (:) and ending with a semicolon (;) although this is optional for the last one., and the declaration groups are surrounded by curly braces {}.

  • Selector: A selector is an HTML tag at which a style will be applied. This could be any tag like <h1> or <table> etc.
  • Property: A property is a type of attribute of HTML tag like color, font-size, border etc.
  • Value: Values are assigned to properties. For example, color property can have the value either red or rgb(125,150,250) etc.
p {
color:green;
}
Try it  
2

Comments

The main use of comments is to clarify the code to developers, including you in the future. They can also be used to improve readability by delimiting sections of the style sheet or providing meta data about the file, such as the author’s name.

Comments in CSS are created by using the C-style notation (/* */). Everything placed between /* and */ will be ignored by browsers, even if the delimiters span multiple lines.
/* This is a CSS comment */
p {
color: red; /* This is a CSS comment */
}

/*
This
is
a
CSS
comment
*/
p {
color: red;
}
Try it  
3

Whitespace

Whitespace refers to spaces, tabs, and new lines. You are free to format your style sheets however you like with whitespace to make them easier to read. One common formatting convention is to split declarations across multiple lines.

p  {
color:    red;
}
Try it  


CSS styles inclusion in HTML

Sl.No. Function Code Reference
1

Inline CSS- The style Attribute

You can use style attribute of any HTML element to define style rules. These rules will be applied to that element only. The style attribute includes a series of CSS property and value pairs. Each property: value pair is separated by a semicolon (;)
<h1 style ="color: blue;">This is inline CSS</h2>
<h1 style ="color: blue;background-color:yellow; ">This is inline CSS</h1>
Try it